package com.fyzk.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fyzk.activity.app.App;
import com.fyzk.activity.app.AppToast;
import com.fyzk.activity.lottery.jc.football.newJz.data.NewScoreDetailActivity;
import com.fyzk.bean.JCLQResponseInfo;
import com.fyzk.interfaces.OnBetCheckedListener;
import com.fyzk.model.JCLQMatchData;
import com.fyzk.net.PublicMethod;
import com.fyzk.utils.LogUtil;
import com.tyjc.lottery.R;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;


public abstract class JCLQBaseAdapter extends BaseExpandableListAdapter {
    protected Activity context;
    protected List<JCLQResponseInfo> infos = new ArrayList<JCLQResponseInfo>();
    protected LayoutInflater miInflater;
    private OnBetCheckedListener listener;
    protected int changCi = 0;
    protected int maxCount = 8;


    public JCLQBaseAdapter(Activity activity, List<JCLQResponseInfo> infos) {
        this.context = activity;
        this.infos = infos;
        this.miInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (App.isBaskOrder) {
            maxCount = 8;
        } else {
            maxCount = 10;
        }
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return infos.get(groupPosition).matchDatas.get(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        final JCLQMatchData data = infos.get(groupPosition).matchDatas
                .get(childPosition);
        final ChildHolder childHolder;
        if (convertView == null) {
            convertView = miInflater.inflate(R.layout.jclq_adapter_child_sf, null);
            childHolder = new ChildHolder();
            childHolder.xianShi = convertView.findViewById(R.id
                    .jc_main_list_item_xianshi);
            childHolder.ll_xiangqing = convertView.findViewById(R.id
                    .jc_main_list_item_yincang);
            childHolder.league_tv = convertView.findViewById(R.id.jclq_child_base_league);
            childHolder.teamId_tv = convertView.findViewById(R.id.jclq_child_base_teamid);
            childHolder.time_tv = convertView.findViewById(R.id.jclq_child_base_time);
            childHolder.home_check = convertView.findViewById(R.id.jclq_child_sf_home);
            childHolder.away_check = convertView.findViewById(R.id.jclq_child_sf_away);
            childHolder.jclq_child_sf_awayname = convertView.findViewById(R.id.jclq_child_sf_awayname);
            childHolder.jclq_child_sf_homename = convertView.findViewById(R.id.jclq_child_sf_homename);
            convertView.setTag(childHolder);
        } else {
            childHolder = (ChildHolder) convertView.getTag();
        }
        childHolder.league_tv.setText(data.getMatchName());
        try {
            String teamId = PublicMethod.getTeamNum(data.getMatchNum());
            childHolder.teamId_tv.setText(data.getOfficialWeekDay() + teamId);
            String endData = new SimpleDateFormat("HH:mm").format(data.getEndSaleTime());
            childHolder.time_tv.setText(endData + "截止");
        } catch (Exception e) {
            LogUtil.e(e.getMessage());
        }
        //初始化选中状态前不让checkbox响应事件
        childHolder.home_check.setOnCheckedChangeListener(null);
        childHolder.away_check.setOnCheckedChangeListener(null);
        JCLQMatchData.SPInfo spInfo_s = data.splInfos.get(data.VS_3);
        JCLQMatchData.SPInfo spInfo_f = data.splInfos.get(data.VS_0);
        childHolder.home_check.setChecked(spInfo_s.isSel);
        childHolder.away_check.setChecked(spInfo_f.isSel);
        childHolder.home_check.setTag(R.string.jc_info, data);
        childHolder.home_check.setTag(spInfo_s);
        childHolder.away_check.setTag(R.string.jc_info, data);
        childHolder.away_check.setTag(spInfo_f);
        childHolder.jclq_child_sf_awayname.setText(data.getAwayTeam() + "[客]");
        childHolder.jclq_child_sf_homename.setText(data.getHomeTeam() + "[主]");
        childHolder.home_check.setText("主胜 " + spInfo_s.spValue);
        childHolder.away_check.setText("客胜 " + spInfo_f.spValue);
        childHolder.home_check.setOnClickListener(clickListener);
        childHolder.away_check.setOnClickListener(clickListener);

        if (data.isShowHideView) {
            childHolder.ll_xiangqing.setVisibility(View.VISIBLE);
        } else {
            childHolder.ll_xiangqing.setVisibility(View.GONE);
        }
        if (data.hideView != null) {

            ViewGroup parent1 = (ViewGroup) data.hideView.getParent();
            if (parent1 != null) {
                parent1.removeAllViews();
            }
            childHolder.ll_xiangqing.removeAllViews();
            childHolder.ll_xiangqing.addView(data.hideView);
        }
        // 点击显示隐藏信息
        childHolder.xianShi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

//                if (data.hideView == null) {
//                    data.hideView = new BasketballMatchSummaryView(context, data.getMatchNum());
//                    childHolder.ll_xiangqing.removeAllViews();
//                    childHolder.ll_xiangqing.addView(data.hideView);
//                }
//                data.isShowHideView = !data.isShowHideView;
//                if (data.isShowHideView) {
//                    childHolder.ll_xiangqing.setVisibility(View.VISIBLE);
//                } else {
//                    childHolder.ll_xiangqing.setVisibility(View.GONE);
//                }
                Intent intent = new Intent(context, NewScoreDetailActivity.class);
                intent.putExtra("isFootball", false);
                intent.putExtra("matchNum", data.getMatchNum());
                context.startActivity(intent);
            }

        });
//        childHolder.ll_xiangqing.setOnClickListener(new View.OnClickListener() {
//
//            @Override
//            public void onClick(View v) {
//                Intent intent = new Intent(context, BasketballScoreDetailActivity.class);
//                intent.putExtra("matchNum", data.getMatchNum());
//                context.startActivity(intent);
//            }
//        });
        return convertView;
    }

    View.OnClickListener clickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            JCLQMatchData.SPInfo spInfo = (JCLQMatchData.SPInfo) v.getTag();
            JCLQMatchData data = (JCLQMatchData) v.getTag(R.string.jc_info);
            spInfo.isSel = !spInfo.isSel;
            int clickNum = data.clickNum;
            if (spInfo.isSel) {
                data.clickNum++;
            } else {
                data.clickNum--;
            }
            if (spInfo.isSel && changCi == maxCount && data.clickNum == 1) {
                AppToast.getToast().show("最多选择" + maxCount + "场");
                spInfo.isSel = false;
                data.clickNum = clickNum;
                ((CheckBox) v).setChecked(false);
            }
            tongJi();
        }
    };

    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
        tongJi();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        // TODO Auto-generated method stub
        try {
            int count = infos.get(groupPosition).matchDatas.size();
            LogUtil.d("getChildrenCount>>>>count===" + count);
            return count;
        } catch (Exception e) {
            // TODO: handle exception
            return 0;
        }
    }

    public interface JLPopOpenListener{
        void open(JCLQMatchData data );
    }

    JLPopOpenListener popOpenListener;

    public void SetJLPopOpenListener(JLPopOpenListener listener){
        this.popOpenListener = listener;
    }

    @Override
    public Object getGroup(int groupPosition) {
        // TODO Auto-generated method stub
        return infos.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        // TODO Auto-generated method stub
        return infos.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        // TODO Auto-generated method stub
        return groupPosition;
    }

    protected View getRightView(final JCLQMatchData data, int position) {
        return null;
    }

    private int lotteryId = 0;

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        JCLQResponseInfo info = infos.get(groupPosition);
        lotteryId = info.lotteryType;
        GroupHolder groupHolder = null;
        if (convertView == null) {
            convertView = miInflater.inflate(R.layout.jclq_adapter_group, null);
            groupHolder = new GroupHolder();
            groupHolder.date_tv = convertView
                    .findViewById(R.id.jclq_group_tv);
            groupHolder.jianTou_iv = convertView
                    .findViewById(R.id.jclq_group_iv);
            convertView.setTag(groupHolder);
        } else {
            groupHolder = (GroupHolder) convertView.getTag();
        }

        groupHolder.date_tv.setText(info.matchTime + "   " + info.matchWeekday
                + "    共 " + info.matchDatas.size() + " 场");
        if (isExpanded) {
            groupHolder.jianTou_iv.startAnimation(AnimationUtils.loadAnimation(
                    context, R.anim.title_jiantou_shun));
        } else {
            groupHolder.jianTou_iv.startAnimation(AnimationUtils.loadAnimation(
                    context, R.anim.title_jiantou_ni));
        }

        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return true;
    }

    protected void tongJi() {
        changCi = 0;
        for (int i = 0; i < infos.size(); i++) {
            for (int j = 0; j < infos.get(i).matchDatas.size(); j++) {
                JCLQMatchData data = infos.get(i).matchDatas.get(j);
                if (data.clickNum > 0) {
                    changCi++;
                }
            }
        }
        if (listener != null) {
            listener.onBetChecked();
        }
    }

    public void setList(List<JCLQResponseInfo> infos) {
        this.infos = infos;
        this.notifyDataSetChanged();
    }

    public void setOnJCLQTongJiListener(OnBetCheckedListener listener) {
        this.listener = listener;
    }

    class GroupHolder {
        TextView date_tv;
        ImageView jianTou_iv;
    }

    class ChildHolder {
        LinearLayout xianShi;
        LinearLayout ll_xiangqing;
        TextView league_tv;
        TextView jclq_child_sf_awayname;
        TextView jclq_child_sf_homename;
        TextView teamId_tv;
        TextView time_tv;
        CheckBox home_check;
        CheckBox away_check;
    }

}
